package org.example.business.endpoint.http.event.message;

import lombok.extern.slf4j.Slf4j;
import org.example.business.endpoint.http.HttpSocket;
import org.example.business.endpoint.http.HttpSocketManager;
import org.example.objevent.event.EventActor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class HttpRspActor implements EventActor<HttpMsg> {

    @Autowired
    private HttpSocketManager socketManager;

    @Override
    public String getWatchEvent() {
        return "HTTP_RSP";

    }

    @Override
    public void act(HttpMsg httpMsg) {
        log.info("处理响应消息 [msgId: {}, socketId: {}]", httpMsg.getMsgId(), httpMsg.getSocketId());

        HttpSocket httpSocket = socketManager.getSocket(httpMsg.getSocketId());

        if(null != httpSocket && HttpSocket.State.Connected == httpSocket.getState()){

            httpSocket.getSocketContext().writeAndFlush(httpMsg);
            httpSocket.getSocketContext().close();

        }

    }

}
